package cate.game.rank.po;

import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;

public class HeroRankSPO extends RankSPO<HeroRank>{
    /**
     * 默认从大到小排
     */
    public void record(Role role, Hero hero, long newValue) {
        // 是否已经上榜
        HeroRank item = findByRoleUid(role.getUid());
        if (item != null && item.value >= newValue) {
            return;
        }
        if (item == null && list.size() >= getRankNum()) {
            HeroRank last = list.get(list.size() - 1);
            if (last.value > newValue) {
                // 没有进排行榜
                return;
            }
        }
        long preValue = item == null ? 0 : item.value;
        Hero newHero = hero.copy();
        if (item != null) {
            // 已经在榜上了
            item.value = newValue;
            item.hero = newHero;
        } else {
            // 之前没有上榜
            HeroRank e = createRank(role, newHero, newValue);
            if (list.size() >= getRankNum()) {
                // 如果排行榜已经满了，那就把最后一名给去掉
                list.remove(list.size() - 1);
            }
            list.add(e);
        }
        if(preValue != newValue){
            sort();
        }
    }

    private HeroRank createRank(Role role, Hero hero, long value) {
        return new HeroRank(role, hero, value);
    }
}
